#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i)  
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;

const int N = 1e6+6;
int n,k,a[2*N],ans,q[2*N],l,r,p[2*N];

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);
    cin >> n >> k;
    rep(i,1,n) cin >> a[i];
    //第一遍，维护最小值
    q[l = r = 1] = a[1];
    rep(i,2,k){
        while(l<=r && a[i]<q[r]) --r;
        q[++r] = a[i] , p[r] = i;
    }
    cout << q[l] << " ";
    rep(i,k+1,n){
        while(l<=r && a[i]<q[r]) --r;
        q[++r] = a[i] , p[r] = i;
        while(p[l]<i-k+1 && l<=r) ++l;
        cout << q[l] << " ";
    }
    cout << "\n";
    //第二遍，维护最大值
    q[l = r = 1] = a[1] , p [r] = 1;
    rep(i,2,k){
        while(l<=r && a[i]>q[r]) --r;
        q[++r] = a[i] , p[r] = i;
    }
    cout << q[l] << " ";
    rep(i,k+1,n){
        while(l<=r && a[i]>q[r]) --r;
        q[++r] = a[i] , p[r] = i;
        while(p[l]<i-k+1 && l<=r) ++l;
        cout << q[l] << " ";
    }
    return 0;
}